//
//  NSString+EJAdd.m
//  JoyssomTool
//
//  Created by LiuShuang on 2019/6/21.
//  Copyright © 2019 LiuShuang. All rights reserved.
//

#import "NSString+EJAdd.h"

@implementation NSString (EJAdd)

- (NSString *)ej_noWhiteSpaceString {
    NSString *newString = self;
    //去除掉首尾的空白字符和换行字符
    newString = [newString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    newString = [newString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    newString = [newString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  //去除掉首尾的空白字符和换行字符使用
    newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""];
    //    可以去掉空格，注意此时生成的strUrl是autorelease属性的，所以不必对strUrl进行release操作！
    return newString;
}

+ (NSString *)ej_getUUID {
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    CFRelease(uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
    CFRelease(uuid_string_ref);
    return uuid;
}

+ (NSString *)ej_shortedNumberDesc:(NSUInteger)number {
    // should be localized
    if (number <= 999) return [NSString stringWithFormat:@"%d", (int)number];
    if (number <= 9999) return [NSString stringWithFormat:@"%d千", (int)(number / 1000)];
    return [NSString stringWithFormat:@"%d万", (int)(number / 10000)];
}

+ (NSString *)ej_shortedCount:(NSUInteger)count {
    if (count > 99) {
        return @"99+";
    } else {
        return [NSString stringWithFormat:@"%d", (int)count];
    }
}

+ (NSString *)ej_shortedSecond:(NSUInteger)second {
    if (second < 60) {
        return [NSString stringWithFormat:@"00:%02d", (int)second];
    } else if (second < 60 * 60) {
        return [NSString stringWithFormat:@"%02d:%02d", (int)second / 60, (int)second % 60];
    } else {
        return [NSString stringWithFormat:@"%02d:%02d:%02d", (int)second / 3600, (int)second % 3600 / 60, (int)second % 3600 % 60];
    }
}

+ (NSString *)ej_getMMSSFromSeconds:(NSUInteger)seconds {
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld", (long)seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld", (long)(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld", (long)seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@", str_hour, str_minute, str_second];
    
    return format_time;
    
}

#pragma mark - emotion
- (BOOL)ej_hasEmoji {
    // 九键单独判断
    if ([self _isNineKey]) {
        return NO;
    }
    // 第三方键盘过滤
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    if (isMatch) {
        return YES;
    }
    
    // 系统键盘上的表情
    __block BOOL returnValue = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    returnValue = YES;
                }
            }
        }
        else if (substring.length > 1) {
            const unichar ls = [substring characterAtIndex:1];
            if (ls == 0x20e3 || ls ==0xfe0f) {
                returnValue = YES;
            }
        }
        else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                returnValue = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}

#pragma mark - private
- (BOOL)_isNineKey {
   NSString * other = @"➋➌➍➎➏➐➑➒";
   int len = (int)self.length;
   for (int i = 0; i < len; i++) {
       if ([other rangeOfString:self].location == NSNotFound)
           return NO;
   }
   return YES;
}

@end
